package com.sy.core.aliPay;

import com.sy.core.model.SyOrderInfo;
import com.sy.core.model.SyOrderInfoDetail;
import com.sy.core.service.OrderInfoDetailService;
import com.sy.core.service.OrderService;
import com.sy.core.web.BaseCtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支付接口
 */
@Controller
@RequestMapping("aliPay")
public class AliPayCtl extends BaseCtl {
	@Autowired
	private OrderInfoDetailService orderInfoDetailService;
	@Autowired
	private OrderService orderService;

	@RequestMapping("pay")
	@Transactional(rollbackFor = Exception.class)
	public String pay(String shoppingCartPks, String addressPk, String pk, SyOrderInfo syOrderInfo, SyOrderInfoDetail syOrderInfoDetail, ModelMap
					  modelMap){
		if(StringUtils.isNotBlank(pk)){
			Map<String, Object> map = orderService.queryOne(pk);
			modelMap.addAttribute("str",(String)map.get("order_record"));
		}else{
			String orderInfoPk = getPrimaryKey();//设置订单主表pk,订单号
			String[] shoppingCartPkList = shoppingCartPks.split(",");
			Map<String, Object> shoppingCart = new HashMap<>();//购物车详情
			String productTotalPrice="0";//这个订单的总价也是所有产品的总价
			String totalPrice = null;//一个产品的总价
			String price = null;//一个产品的单价
			String account=null;//产品数量
			for (int i = 0; i <shoppingCartPkList.length ; i++) {
				String orderInfoDetailPk = getPrimaryKey();
				shoppingCart = shoppingCartService.queryOne(shoppingCartPkList[i]);
				syOrderInfoDetail.setPk(orderInfoDetailPk);//设置订单子表主键
				syOrderInfoDetail.setProductPk((String)shoppingCart.get("no1"));
				syOrderInfoDetail.setSkuPk((String)shoppingCart.get("product_sku_pk"));
				syOrderInfoDetail.setProductName((String)shoppingCart.get("product_name"));
				syOrderInfoDetail.setProductPic((String)shoppingCart.get("product_avatar"));
				syOrderInfoDetail.setSkuName((String)shoppingCart.get("sku_name"));
				account=(String)shoppingCart.get("product_sku_count");
				syOrderInfoDetail.setAccount(account);
				String[] path = ((String) shoppingCart.get("product_avatar")).split(";");
				syOrderInfoDetail.setProductPic(path[0]);
				if(("-1").equals((String)shoppingCart.get("product_sku_pk"))){//如果sku为-1那么是个产品，不是sku
					price = (String)shoppingCart.get("product_sale");
					syOrderInfoDetail.setProductPrice(price);
				}else{
					price=(String)shoppingCart.get("sku_off_price");
					syOrderInfoDetail.setProductPrice(price);
				}
				totalPrice = new BigDecimal(price.replaceAll(",","")).multiply(new BigDecimal(account)).toString();
				productTotalPrice=new BigDecimal(productTotalPrice).add(new BigDecimal(totalPrice)).toString();
				syOrderInfoDetail.setCreateBy(getLoginUser().getPk());
				syOrderInfoDetail.setCreateTime(getTime());
				syOrderInfoDetail.setOrderPk(orderInfoPk);
				syOrderInfoDetail.setC1(totalPrice);
				orderInfoDetailService.insert(syOrderInfoDetail);//把所有商品的详情插入订单子表
			}
            String str = PayUtil.alipay(orderInfoPk, productTotalPrice, "电子商品", "");//调用支付宝支付接口
            Map<String, Object> address = addressService.queryOne(addressPk);//查询该订单的地址信息
			syOrderInfo.setPk(orderInfoPk);
			syOrderInfo.setOrderTime(getTime());
			syOrderInfo.setTotalPrice(productTotalPrice);
			syOrderInfo.setOrderStatus(1);
			syOrderInfo.setOrderPersion(getLoginUser().getPk());
			syOrderInfo.setOrderPersionName(getLoginUser().getName());
			syOrderInfo.setOrderRecord(str);//支付记录
			syOrderInfo.setOrderAddressPk((String)address.get("receiver_province")+(String)address.get("receiver_city")+
                    (String)address.get("receiver_county")+(String)address.get("receiver_address"));
			syOrderInfo.setC3((String)address.get("receiver_name"));
            syOrderInfo.setC4((String)address.get("receiver_phone"));
			orderService.insert(syOrderInfo);//把支付信息插入订单主表
			shoppingCartService.batchDel(shoppingCartPks);
			modelMap.addAttribute("str",str);
		}
		return "/website/pay";
	}
}
